package com.tl.commonlibrary.ui;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v4.content.ContextCompat;
import android.widget.AdapterView;

import com.tl.commonlibrary.R;
import com.tl.commonlibrary.ui.adapter.ListViewAdapter;
import com.tl.commonlibrary.ui.widget.pull.PullToRefreshLayout;
import com.tl.commonlibrary.ui.widget.pull.PullableListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jiangzhongshan on 18/2/4.
 */

public abstract class BaseListViewFragmentActivity<T> extends BaseFragmentActivity implements PullToRefreshLayout.OnRefreshListener, AdapterView.OnItemClickListener, ListViewAdapter.OnListViewAdapterListener<T> {
    protected PullableListView listView;
    protected PullToRefreshLayout controller;
    protected int page = 1;
    protected ArrayList<T> datas = new ArrayList<>();
    protected ListViewAdapter<T> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pull_list_view_ui_layout);
        initListViewUI();
    }

    protected void initListViewUI() {
        listView = (PullableListView) findViewById(R.id.listView);
        controller = (PullToRefreshLayout) findViewById(R.id.listViewController);
        listView.setOnItemClickListener(this);
        controller.setOnRefreshListener(this);
        adapter = new ListViewAdapter<T>(context, null, getItemLayoutResId(), this);
        listView.setAdapter(adapter);
//        adapter.setListViewAdapterListener(this);
    }

    protected void autoRefresh() {
        listView.postDelayed(new Runnable() {
            @Override
            public void run() {
                controller.autoRefresh();
            }
        }, 200);
    }

    protected void addDatas(List<T> datas) {
        adapter.addData(datas);
    }

    protected void addData(T data) {
        adapter.addData(data);
    }

    protected void setDatas(List<T> datas) {
        adapter.setDatas(datas);
    }

    protected void notifyDataSetChanged() {
        adapter.notifyDataSetChanged();
    }

    protected void nextPage() {
        page++;
    }

    protected void resetPage() {
        page = 1;
    }

    protected int getPage() {
        return page;
    }

    protected void requestSuccess() {
        if (controller != null) {
            controller.finish(PullToRefreshLayout.SUCCEED);
        }
    }

    protected void requestFailed() {
        if (controller != null) {
            controller.finish(PullToRefreshLayout.FAIL);
        }
    }

    /**
     * @param canPullDown 是否允许刷新
     * @param canPullUp   是否允许加载更多
     */
    protected void enablePull(boolean canPullDown, boolean canPullUp) {
        controller.enablePullDown(canPullDown);
        controller.enablePullUp(canPullUp);
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout, boolean isAuto) {
        resetPage();
        requestData(true);
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout, boolean isAuto) {
        requestData(false);
    }

    protected void setListDividerHeightTransparent(int height) {
        listView.setDivider(new ColorDrawable(ContextCompat.getColor(context, R.color.transparent)));
        listView.setDividerHeight(height);
    }
    protected void setListViewPadding(int left,int top,int right,int bottom){
        listView.setPadding(left,top,right,bottom);
    }
    public abstract @LayoutRes
    int getItemLayoutResId();

    public abstract void requestData(boolean isRefersh);
}
